package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.chatuidemo.DemoApplication;
import com.hyphenate.easeui.adapter.RecommandCardAdapter2;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.exceptions.HyphenateException;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.adapter.RecommandCardAdapter;
import com.joplus.housekeeper.bean.ChatCard;
import com.joplus.housekeeper.bean.RecommandCardBean;
import com.joplus.housekeeper.network.RequestHelper;
import com.joplus.housekeeper.network.TextCallback;
import com.joplus.housekeeper.ui.view.CustomNoScrollListView;
import com.joplus.housekeeper.utils.DensityUtils;
import com.joplus.housekeeper.utils.L;

import org.json.JSONObject;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * 推荐卡片
 */
public class EaseChatRowText_RecommandCard extends EaseChatRow {

    private TextView contentView;
    RecommandCardBean bean;
    RecyclerView recyclerView;
    RecommandCardAdapter adapter;
    CircleImageView headView;//不能直接删除，框架里面其他地方有引用

    private static Boolean isani = new Boolean(false);
    private CustomNoScrollListView ll__rommandr_card;


    public EaseChatRowText_RecommandCard(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);


    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_recommand_card : R.layout.ease_row_sent_message, this);


    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);
        headView = (CircleImageView) findViewById(R.id.iv_userhead);
        //card
        //  recyclerView = (RecyclerView) findViewById(R.id.rv_rommandr_card);
        ll__rommandr_card = (CustomNoScrollListView) findViewById(R.id.ll_rommandr_card);
//        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(getWidth() , ViewGroup.LayoutParams.MATCH_PARENT);
//        recyclerView.setLayoutParams(params);
    /*    RecyclerView.LayoutManager lm = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(lm);
        recyclerView.addItemDecoration(new InsetDecoration(context));*/

        if (!isani) {
            isani = true;
            int fx = DensityUtils.dip2px(context, 500);
            TranslateAnimation translateAnimation = new TranslateAnimation(fx, 0, 0, 0);
            translateAnimation.setDuration(1000);
            ll__rommandr_card.startAnimation(translateAnimation);
        }

    }

    @Override
    public void onSetUpView() {
        /*if (message.getBody()!=null){*/
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
        contentView.setText(span, BufferType.SPANNABLE);
        String msg = txtBody.getMessage();
        headView.setVisibility(GONE);
        try {
            new JSONObject(msg);
            Gson gson = new Gson();
            ChatCard chatCard = gson.fromJson(txtBody.getMessage(), ChatCard.class);
            bubbleLayout.setVisibility(View.GONE);
            contentView.setVisibility(View.GONE);
            setCard(chatCard.getRecommandCardBean());

        } catch (Exception e) {

            bubbleLayout.setVisibility(View.VISIBLE);
            contentView.setVisibility(View.VISIBLE);

        }
//        handleTextMessage();

    }

    private void setCard(final RecommandCardBean chatCard) {


        //  adapter = new RecommandCardAdapter(context, chatCard.getData().getRecommendList());
        // recyclerView.setAdapter(adapter);
        ll__rommandr_card.setAdapter(new RecommandCardAdapter2(context, chatCard.getData().getRecommendList()));


    }


    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    // 发送消息
                    //               sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        //   adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        L.e("点击了聊天文字");
    }


    /**
     * 判断是否是我自己的卡片，可能是后台发错了！
     *
     * @return
     */
    private boolean isMyCard(String cardaccoundId) {
        if (cardaccoundId != null) {
            return cardaccoundId.equals(DemoApplication.getInstance().getUser().getData().getAccountId());
        }
        return true;
    }


    public static void insertRecommCard(final Context context, String id, final InsertRecommandCard i) {


    }

    public static interface InsertRecommandCard {
        void insertMessage(String message);
    }


}
